<?php
/*
 * @Author: your name
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2022-03-06 23:11:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\admin_controller;

// use App\Models\admin_model\Admins;                        // admins表
use App\Models\admin_model\Column;                        // admins表
use App\Http\Controllers\redis\RedisController as Redis;  //redis 缓存
use App\Http\Controllers\admin_controller\Login;  //redis 缓存

class CheckRole extends BaseController
{
    /**
     * @description: 登录接口
     */
    public function checkRole()
    {
        $admin = $this->getAdminInfo();
        if ($admin['status']  == 0) {
            return $this->create(-1, '该账号已被禁用');
        } else if ($admin['delete_time']) {
            return $this->create(-1, '该账号已被删除');
        }else if($admin['roleId'] ==  -1 ){
            $loginObj = new Login;
            $loginObj->loginOut(false);   //退出登录
            return $this->create(100, '该账号没有权限进入');
        }

        if ($admin['roleId'] == 0) {
            //超级管理员
            $permissions =  Column::columnAndPermission();
            for ($i = 0; $i < count($permissions); $i++) {
                if (!empty($permissions[$i]['operation'])) {
                    $permissions[$i]['operation'] = explode(',', $permissions[$i]['operation']);
                }
            }
        } else { 
            $redisKey = 'role_'.$admin['roleId'];  //键值
            $redisHname = 'PERMISSIONS' ;          //哈希键值名称
            $res = Redis::hGET( $redisKey,$redisHname  ) ;
            if( $res){
                $permissions =   unserialize( $res );
            }else{
                $roleArray = redis_mysql_selone_return('role', $admin['roleId'], ['column_id', 'permission_id']);
                $permissions = self::format($roleArray);
                Redis::hMset($redisKey,[$redisHname=> serialize( $permissions )]);  //第一次存起来
            }
        }
        return $this->create(200, '查询成功', ['user' => [
            // 'address' => "重庆市",
            'avatar' =>  $admin['avatar'],
            'name' =>  $admin['nickName'],
        ], 'permissions' =>  $permissions]);
    }


    public function getRoute()
    {
        $columnAll =  Column::columnAll();
        $columnList =  $this->getTree( $columnAll);
        // echo 111;
        return $this->create(200, '后台还在登录中...', $columnList);
    }





    private function getTree($column)
    {
        $column_tmp = [];
        $column_last = [];
        foreach ($column as $val) {
            $val['authority'] = [
                'permission' => $val['router'],
            ];
            $column_tmp[$val['id']] = $val;
        }
        foreach ($column_tmp as  &$val) {
            if ($val['pid'] == 0) {
                $column_last[] = &$val;
            } else {
                $column_tmp[$val['pid']]['children'][] =  &$val;
                $sort = array_column($column_tmp[$val['pid']]['children'], 'sort');              //提取数组某个字段
                array_multisort($sort, SORT_NUMERIC, $column_tmp[$val['pid']]['children']);     //按某个字段降序
            }
        }
        $sort = array_column($column_last, 'sort');              //提取数组某个字段
        array_multisort($sort, SORT_NUMERIC, $column_last);      //按某个字段降序
        return   $column_last;
    }
     
     /**
      * @description:     前端权限格式
      * @param  array $roleArray
      * @return $permissions
      */    
     public static function format($roleArray){
        $role = Column::checkRole(!empty($roleArray['column_id'])?$roleArray['column_id']:'', !empty($roleArray['permission_id'])?$roleArray['permission_id']:'' );
        $permissions = [];
        for ($i = 0; $i < count($role); $i++) {
            $tmp = [];
            $tmp['id'] = $role[$i]['router'];
            $tmp['operation'] = explode(',', $role[$i]['operation']);
            if (!empty($role[$i]['operation_router'])) {
                $operation_router   = explode(',', $role[$i]['operation_router']);
                for ($j = 0; $j < count($operation_router); $j++) {
                    $permissions[]['id'] =   $operation_router[$j];
                }
            }
            $permissions[] = $tmp;
        }

        return  $permissions;
     }
  
}
